package com.junxonline.plat.service.category.impl;

import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.dao.model.Category;
import com.junxonline.plat.dao.model.Charge;
import com.junxonline.plat.dao.repository.CategoryRepository;
import com.junxonline.plat.dao.repository.HouseholdRepository;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.PlatBizException;
import com.junxonline.plat.service.category.CategoryService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    private Logger logger = LogManager.getLogger();

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private HouseholdRepository householdRepository;

    /**
     * 获取收费类别数据
     *
     * @param vo
     * @return
     */
    @Override
    public CategoryVO getCategory(QueryCategoryVO vo) {

        Category category = categoryRepository.findByCategoryId(vo.getCategoryId());

        CategoryVO categoryVO = new CategoryVO();
        categoryVO.setCategoryId(category.getCategoryId());
        categoryVO.setCategoryName(category.getCategoryName());
        categoryVO.setCategoryDesc(category.getCategoryDesc());
        categoryVO.setCategoryUnit(category.getCategoryUnit());
        categoryVO.setCategoryPrice(category.getCategoryPrice());

        return categoryVO;
    }

    /**
     * 筛选查询收费类别
     *
     * @param pageVO
     * @param vo
     * @return
     */
    @Override
    public PageVO queryCategory(PageVO pageVO, QueryCategoryVO vo) {

        PageRequest pageRequest = PageRequest.of(pageVO.getPage() - 1, pageVO.getLimit(), new Sort(Sort.Direction.ASC, "categoryId"));

        Page<Category> page = categoryRepository.findAll(new Specification<Category>() {
            @Override
            public Predicate toPredicate(Root<Category> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                List<Predicate> params = new ArrayList<>();
                String categoryName = vo.getCategoryName();

                if (CommonUtils.isNotEmpty(categoryName)) {
                    params.add(criteriaBuilder.like(root.get("categoryName"), "%" + categoryName + "%"));
                }

                return criteriaBuilder.and(params.toArray(new Predicate[params.size()]));
            }
        }, pageRequest);

        List<CategoryVO> categoryVOs = new ArrayList<>();
        List<Category> categorys = page.getContent();

        for (Category category : categorys) {

            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setCategoryName(category.getCategoryName());
            categoryVO.setCategoryId(category.getCategoryId());
            categoryVO.setCategoryDesc(category.getCategoryDesc());
            categoryVO.setCategoryUnit(category.getCategoryUnit());
            categoryVO.setCategoryPrice(category.getCategoryPrice());

            categoryVOs.add(categoryVO);
        }

        pageVO.setTotal(page.getTotalElements());
        pageVO.setData(categoryVOs);

        return pageVO;

    }

    /**
     * 添加收费类别
     *
     * @param vo
     * @return
     */
    @Override
    public boolean addCategory(CategoryVO vo) {

        Category category = new Category();
        category.setCategoryName(vo.getCategoryName());
        category.setCategoryDesc(vo.getCategoryDesc());
        category.setCategoryUnit(vo.getCategoryUnit());
        category.setCategoryPrice(vo.getCategoryPrice());

        categoryRepository.save(category);

        return true;
    }

    /**
     * 修改收费类别
     *
     * @param vo
     * @return
     */
    @Override
    public boolean editCategory(CategoryVO vo) {

        Category category = categoryRepository.findByCategoryId(vo.getCategoryId());

        category.setCategoryName(vo.getCategoryName());
        category.setCategoryDesc(vo.getCategoryDesc());
        category.setCategoryUnit(vo.getCategoryUnit());
        category.setCategoryPrice(vo.getCategoryPrice());

        categoryRepository.save(category);

        return true;
    }

    /**
     * 删除收费类别
     *
     * @param categoryIds
     * @return
     */
    @Override
    public boolean delCategory(Long[] categoryIds) {

        List<Category> categorys = categoryRepository.findByCategoryIdIn(categoryIds);

        for (Category category : categorys) {

            // 如果该类别有收费记录在使用 则不允许删除
            List<Charge> charges = category.getCharges();
            if (CommonUtils.isNotEmpty(charges)) {
                throw PlatBizException.CHARGE_HAVE_CATEGORY_DELETE_ERROR;
            }

            // 删除收费类别
            categoryRepository.delete(category);
        }


        return true;
    }

}
